﻿Imports System.Globalization
Imports Aricie.Services
Imports DotNetNuke.Modules.Blog.Business

Namespace Aricie.DNN.Services.Url.FriendlyUrl.Providers

    Public Class BlogUrlRewriter
        Inherits SimpleAPI.SimpleRewriterProvider
        Implements IIsActive


        Protected Overrides Function GetRewritingGroups() As System.Collections.Generic.List(Of SimpleAPI.SimpleGroupParamsRewrite)
            Dim results As New List(Of SimpleAPI.SimpleGroupParamsRewrite)
            results.Add(SimpleAPI.SimpleGroupFluentFactory.CreateGroup("BlogTitle", "Title of current blog") _
                        .AddParameter("BlogId") _
                        .SetRewritingDelegate(AddressOf GetBlogTitle) _
                        .Create())
            results.Add(SimpleAPI.SimpleGroupFluentFactory.CreateGroup("ParentBlogTitle", "Title of parent blog") _
                        .AddParameter("ParentBlogId") _
                        .SetRewritingDelegate(AddressOf GetBlogTitle) _
                        .Create())

            results.Add(SimpleAPI.SimpleGroupFluentFactory.CreateGroup("BlogEntryTitle", "Title of current entry") _
                        .AddNonExclusiveParameter("EntryId") _
                        .SetRewritingDelegate(AddressOf GetEntryTitle) _
                        .Create())
            results.Add(SimpleAPI.SimpleGroupFluentFactory.CreateGroup("BlogEntryDate", "Date of current entry") _
                        .AddNonExclusiveParameter("EntryId") _
                        .SetRewritingDelegate(AddressOf GetEntryDate) _
                        .Create())

            results.Add(SimpleAPI.SimpleGroupFluentFactory.CreateGroup("BlogArchive", "Archive visualisation for blogs") _
                        .AddParameter("BlogDate") _
                        .AddParameter("DateType") _
                        .SetRewritingDelegate(AddressOf GetArchiveDate) _
                        .Create())

            Return results
        End Function

        Private Function GetBlogTitle(ByVal CurrentGroup As SimpleAPI.SimpleGroupParamsRewrite, ByVal objRewriteType As RewriteType, ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String), ByVal strategy As FriendlierUrlStrategy) As String
            Return New BlogController().GetBlog(CurrentGroup.SingleParameterAsInteger(params)).Title
        End Function

        Private Function GetEntryTitle(ByVal CurrentGroup As SimpleAPI.SimpleGroupParamsRewrite, ByVal objRewriteType As RewriteType, ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String), ByVal strategy As FriendlierUrlStrategy) As String
            Return New EntryController().GetEntry(CurrentGroup.SingleParameterAsInteger(params), DotNetNuke.Entities.Portals.PortalController.GetCurrentPortalSettings().PortalId).Title
        End Function

        Private Function GetEntryDate(ByVal CurrentGroup As SimpleAPI.SimpleGroupParamsRewrite, ByVal objRewriteType As RewriteType, ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String), ByVal strategy As FriendlierUrlStrategy) As String
            Return New EntryController().GetEntry(CurrentGroup.SingleParameterAsInteger(params), DotNetNuke.Entities.Portals.PortalController.GetCurrentPortalSettings().PortalId).AddedDate.ToString("yyyy/MM/dd")
        End Function

        Private Function GetArchiveDate(ByVal CurrentGroup As SimpleAPI.SimpleGroupParamsRewrite, ByVal objRewriteType As RewriteType, ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String), ByVal strategy As FriendlierUrlStrategy) As String
            Dim BlogDate As DateTime = DateTime.ParseExact(CurrentGroup.GetParameter("BlogDate", params), "yyyy-MM-dd", CultureInfo.InvariantCulture)
            Dim DateType As String = CurrentGroup.GetParameter("DateType", params)
            Select Case DateType.ToLowerInvariant
                Case "day"
                    Return BlogDate.ToString("yyyy/MM/dd")
                Case "month"
                    Return BlogDate.ToString("yyyy/MM")
                Case Else
                    Return BlogDate.ToString("yyyy/MM/dd")
            End Select
        End Function

        Public ReadOnly Property IsActive As Boolean Implements IIsActive.IsActive
            Get
                Return ReflectionHelper.CreateType("DotNetNuke.Modules.Blog.Business.BlogController, DotNetNuke.Modules.Blog", False) IsNot Nothing
            End Get
        End Property
    End Class

End Namespace